package net.lcheng.shop.Controller;

import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.BaseAreas;
import net.lcheng.model.User;
import net.lcheng.model.UserAgent;
import net.lcheng.service.BaseAreasService;
import net.lcheng.service.UserAgentService;
import net.lcheng.service.UserService;
import net.lcheng.shop.vo.CurrentUserModel;
import net.lcheng.vo.UserAgentModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.Date;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/11 0011.
 */
@Controller
public class UserAgentController {
    @Autowired
    private UserAgentService userAgentService;
    @Autowired
    private BaseAreasService baseAreasService;
    @Autowired
    private UserService userService;
    /***
     * 代理申请get
     */
    @RequestMapping(value = "user/agent_application", method = RequestMethod.GET)
    public String userAgentGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user = userService.getUserByUserId(currentUserModel.getUserId());
            if (user==null){
                SecurityUtils.getSubject().logout();
                return "redirect:/login";
            }
            UserAgent userAgent=userAgentService.getUserAgentByUserId(user.getUserId());
            if (userAgent!=null){
                model.addAttribute("userIdentity",1);//用户身份，1已申请
            }else
                model.addAttribute("UserAgentModel", new UserAgentModel());
        } else {
            return "redirect:/login";
        }
        return "user_agent";
    }
    /***
     * 代理申请post
     */
    @RequestMapping(value = "user/agent_application", method = RequestMethod.POST)
    public String userAgentPost(Model model, @Valid @ModelAttribute("UserAgentModel") UserAgentModel userAgentModel, BindingResult result) {
        Subject currentUser = SecurityUtils.getSubject();
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user = userService.getUserByUserId(currentUserModel.getUserId());
           UserAgent userAgentByUserId=userAgentService.getUserAgentByUserId(user.getUserId());
        if (result.hasErrors()) {
            return "user_agent";
        }
        /**获取省名称*/
        BaseAreas provinceCode = baseAreasService.getAreaCodeByAreaName(userAgentModel.getAddrProvinceCode());
        /**获取市名称*/
        BaseAreas cityCode = baseAreasService.getAreaCodeByAreaName(userAgentModel.getAddrCityCode());
        /**获取区名称*/
        BaseAreas districtCode = baseAreasService.getAreaCodeByAreaName(userAgentModel.getAddrDistrictCode());
        if (userAgentByUserId!=null){
            return "redirect:/user/agent_application";
        }else
        //判断以前有没有申请过代理申请如果没有申请过才能申请
        if (userAgentByUserId==null){
            UserAgent userAgent=new UserAgent();
            userAgent.setAgentId(UUID.randomUUID().toString());
            userAgent.setUserUserId(currentUserModel.getUserId());
            userAgent.setAgentName(userAgentModel.getAgentName());//代理名
            userAgent.setPhone(userAgentModel.getPhone());//联系手机
            userAgent.setApplyReason(userAgentModel.getApplyReason());//申请理由
            userAgent.setAddrProvinceCode(userAgentModel.getAddrProvinceCode());//省编码
            userAgent.setAddrProvinceName(provinceCode.getAreaName());//省名字
            userAgent.setAddrCityCode(userAgentModel.getAddrCityCode());//市编码
            userAgent.setAddrCityName(cityCode.getAreaName());
            userAgent.setAddrDistrictCode(userAgentModel.getAddrDistrictCode());
            userAgent.setAddrDistrictName(districtCode.getAreaName());
            userAgent.setAddress(userAgentModel.getAddress());//地址
            userAgent.setApplyStatus(0);
            userAgent.setPayStatus(0);
            userAgent.setAgentStatus(0);
            userAgent.setCreateOn(new Date());
            int returnResult=userAgentService.insert(userAgent);
            if (returnResult>0){
                model.addAttribute("returnResult",1);
            }
        }
        return "user_agent";
    }

}
